package com.shaoshaohuo.app.ui.ec;

import java.util.ArrayList;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.SupplyRecord;
import com.shaoshaohuo.app.entity.SupplyRecordEntity;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.SupplyRecordListAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author: yusonglin
 * @类 说 明:
 * 
 *  供应记录
 * @version 1.0
 * @创建时间：2015年8月31日 上午10:23:36
 * 
 */
public class SupplyRecordActivity extends BaseActivity implements IXListViewListener {

	private TopbarView mTopbarView;
	private XListView mListView;

	private SupplyRecordListAdapter<SupplyRecord> mAdapter;

	private List<SupplyRecord> mDataList = new ArrayList<SupplyRecord>();
	private int size = 10;
	private String action = ListViewAction.NEW;
	private String cursor = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(new TopbarView(this));
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				setContentView(R.layout.activity_supply_record);
				initView();
				setUpView();
				requestData(true);
			}
		}, 200);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		mListView = (XListView) this.findViewById(R.id.listview_data_list);
	}

	private void setUpView() {
		mTopbarView.setCenterText("供应记录");
		mTopbarView.setLeftView(true, true);

		// List<String> list = new ArrayList<>();
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		// list.add("");
		//
		// SupplyRecordListAdapter<String> adapter = new
		// SupplyRecordListAdapter<>(this, list, false);
		// mListView.setAdapter(adapter);
		//
		// mListView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view, int
		// position, long id) {
		// Intent intent = new
		// Intent(SupplyRecordActivity.this,PurchaseDetailActivity.class);
		// startActivity(intent);
		// }
		// });

		setListView();
	}

	private void requestData(boolean isLoading) {
		if (isLoading) {
			startLoadingDialog();
		}
		RequestService.getInstance().supplyPublishMyList(this, cursor, size + "", action, SupplyRecordEntity.class,
				new RequestListener() {

					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						dismissLoadingDialog();
						if (resultData.isOk()) {
							SupplyRecordEntity entity = (SupplyRecordEntity) resultData;
							List<SupplyRecord> list = entity.getData().getList();
							mDataList.addAll(list);

							if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
								// mListView.setPullLoadEnable(false);
								mListView.endLoadingMore("没有更多了");
							}
							setAdapter(mDataList);

						} else {
							showToast(resultData.getMsg());
						}
						onLoad();
					}

					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
						dismissLoadingDialog();
						showToast(msg);
						onLoad();
					}
				});
	}

	protected void setAdapter(List<SupplyRecord> mDataList2) {
		// if(mDataList2.isEmpty()){
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// mDataList2.add(new SupplyRecord());
		// }
		if (mAdapter == null) {
			mAdapter = new SupplyRecordListAdapter<SupplyRecord>(this, mDataList2, false);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.setData(mDataList2);
		}
	}

	private void setListView() {
		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
		mListView.setXListViewListener(this);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				SupplyRecord order = (SupplyRecord) parent.getItemAtPosition(position);
				// Intent intent = new
				// Intent(PurchaseRecordActivity.this,OrderDetailShipperActivity.class);
				// intent.putExtra(ExtraName.orderid, order.getBusinessorderid());
				// startActivity(intent);
				Intent intent = new Intent(SupplyRecordActivity.this, SupplyDetailActivity.class);
				intent.putExtra(ExtraName.id, order.getId());
				intent.putExtra("myinfo", true);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onRefresh() {
//		if (mDataList.size() > 0) {
//			cursor = mDataList.get(0).getCursor();
//		} else {
			cursor = "0";
//		}
			mDataList.clear();
		action = ListViewAction.NEW;
		mListView.setPullLoadEnable(true);
		requestData(false);
	}

	@Override
	public void onLoadMore() {
		if (mDataList.size() > 0) {
			cursor = mDataList.get(mDataList.size() - 1).getCursor();
		} else {
			cursor = "0";
		}
		action = ListViewAction.HISTORY;
		requestData(false);
	}

	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
	}

}
